package com.lwl.service.impl;


import com.lwl.entity.Tuser;
import com.lwl.dao.TuserMapper;
import com.lwl.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import tk.mybatis.mapper.entity.Example;


/**
 * @author Lei
 * @create 2022-10-19 12:34
 */

@Service
public class LoginServiceImpl implements ILoginService {

    @Autowired
    private TuserMapper tuserMapper;

    /**
     *  查询登录用户是否存在
     */
    @Override
    public Tuser loginTest(Tuser tuser) {

        Example example = new Example(Tuser.class);
        example.createCriteria().andEqualTo("username",tuser.getUsername());
//        查询登录状态为1（启用）的用户
        example.and().andEqualTo("status",1);
//        1、查询该用户是否存在
        Tuser tuser1 = tuserMapper.selectOneByExample(example);
//        对比该用户的数据库密码与页面输入的密码是否一致
        if (tuser1!=null){
            String password = tuser.getPassword();
            String salt = tuser1.getSalt();
//            同样的方式加密获得对应的加密密码
            String s = DigestUtils.md5DigestAsHex((password + salt).getBytes());
            if (s.equals(tuser1.getPassword())){
                return tuser1;
            }else {
                return null;
            }
        }
        return null;
    }

}
